//
//  HWGameCommentKeyboardView.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/24.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift
class HWGameCommentKeyboardView: UIView {
    var bgView:UIView!
    var sendButton:UIButton!
    var textField:UITextField!
    var sendText:((_:String)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        sendButton.layoutIfNeeded()

        sendButton.layer.cornerRadius = 4*widthScale
        sendButton.layer.masksToBounds = true
        
        UIColor.gradientColor(view: sendButton, colors: [
            UIColor.colorWithAlphaHexString(hexString: "#FFB60A", alpha: 1).cgColor,
            UIColor.colorWithAlphaHexString(hexString: "#F66A05", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 0))
    }
}

extension HWGameCommentKeyboardView {
    private func initUI() -> Void {
        backgroundColor = .white
        
        IQKeyboardManager.shared.toolbarBarTintColor = .white
        IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "收起"
        IQKeyboardManager.shared.keyboardDistanceFromTextField = 100

        bgView = UIView()
        bgView.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        bgView.layer.cornerRadius = 4*widthScale
        bgView.layer.masksToBounds = true
        addSubview(bgView)
        
        textField = UITextField()
        textField.font = UIFont.systemFont(ofSize: 14*widthScale)
        textField.textColor = .colorWithHexString(hexString: "#333333")
        textField.placeholder = "把你想说的写在这里..."
        textField.tintColor = .white
        bgView.addSubview(textField)
        
        sendButton = .init(type: .custom)
        sendButton.setTitle("发送", for: UIControl.State.normal)
        sendButton.setTitleColor(.white, for: UIControl.State.normal)
        sendButton.titleLabel?.font = .systemFont(ofSize: 16*widthScale)
        sendButton.backgroundColor = UIColor.blue
        sendButton.layer.cornerRadius = 4*widthScale
        sendButton.addTarget(self, action: #selector(sendButtonClick), for: UIControl.Event.touchUpInside)
        addSubview(sendButton)
    }
    
    private func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.height.equalTo(36*widthScale)
            make.left.equalTo(11*widthScale)
            make.top.equalTo(12*widthScale)
            make.right.equalTo(sendButton.snp.left).offset(-10*widthScale)
            make.bottom.equalTo(-12*widthScale)
        }
        
        textField.snp.makeConstraints { make in
            make.left.equalTo(10*widthScale)
            make.top.equalTo(5*widthScale)
            make.bottom.right.equalTo(-5*widthScale)
        }
        
        sendButton.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.height.equalTo(36*widthScale)
            make.width.equalTo(63*widthScale)
            make.top.equalTo(bgView)
        }
        
        sendButton.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.horizontal)
        sendButton.setContentCompressionResistancePriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.horizontal)
    }
}


extension HWGameCommentKeyboardView {
    @objc func sendButtonClick() -> Void {
        sendText?(textField.text ?? "")
    }
}


